import AV from 'leancloud-storage';



export const initLeanCloud = () => {
    var APP_ID = 'mFqiJrFp0LFVmus00vUUyoUh-gzGzoHsz';
    var APP_KEY = 'IGyNU1RHgsAw3Rn3n8Av9xwi';
    AV.init({
        appId: APP_ID,
        appKey: APP_KEY
    });
    console.log('leancloud 初始化完成')
}

export const test = () => {
    var TestObject = AV.Object.extend('TestObject');
    var testObject = new TestObject();
    testObject.save({
        words: 'Hello World!'
    }).then(function (object) {
        alert('LeanCloud Rocks!');
    })
}

export const login = (username, pwd) => {
    return AV.User.logIn(username, pwd);
}


export const regist = (username, pwd, email) => {
    // 新建 AVUser 对象实例
    var user = new AV.User();
    // 设置用户名
    user.setUsername(username);
    // 设置密码
    user.setPassword(pwd);
    // 设置邮箱
    user.setEmail(email);
    return user.signUp();
}


export const checkSession = (callback) => {
    var currentUser = AV.User.current();
    if (currentUser) {
        console.log(currentUser)
        currentUser.isAuthenticated().then(state => {
            callback(state)
        });
    } else {
        callback(false)
    }
}


export const currentUser = () => {
    return AV.User.current();
}


export const logout = () => {
    AV.User.logOut();
}